import { defineStore } from 'pinia'

import { message } from 'ant-design-vue'

import { usePermissionStore } from '@/store'
import * as userApi from '@/api/user'

interface UserConfigInfo {
  id?: string
  outUserId?: string
  appKey?: string
  secretKey?: string
  status?: number
}
interface UserInfo {
  autoQuotationStatus?: number
  balance?: number
  createTime?: string
  id?: string
  imServiceSeat?: string
  usable?: number
  userConfigInfo?: UserConfigInfo
  closeStatus?: number
  userName?: string
  showLimitTime?: number
  quotedHistoryTime?: number
  role: string
}
let userInfo = ref<UserInfo>({})

export const useUserStore = defineStore('user', {
  state: () => ({
    token: '',
    userInfo,
    menuRole: [
      'account',
      'questionAnswer',
      'intelligentQA',
      'fans',
      'IMCustomerService',
      'financialCenter',
      'quotationRules',
      'systemSetting',
      'order',
      'xyPlatform',
      'workPlace',
      'dataCenter',
      'wx',
      'ocrRecord',
      'xyRefundOrder',
      'promotionCenter',
      'rules',
      // 'cardCoupons', // 91卡券
      // 'cardCouponsXC', // 星驰卡券
      'cardResource', // 卡券资源
      'movieTicket', //影票代订
      'physicalDistribution', // 实物尾货分销
      'lifeSpecials',
      // 'userPedigree',
      // 'offerRecord'
      // 'assetsManage', // 资产管理
    ],
    nowRouter: '',
    newRouterList: [
      '/user-list',
      '/order-manage-list',
      '/assets-manage',
      '/admin-financial-detail',
      '/admin-account',
      '/promotion-center',
      '/integral-product',
      '/user-pedigree',
      '/user-black',
    ],
    userLoginValue: 0, // 新的登录页面 的判断标识
    ProductRobotList: [],
  }),

  actions: {
    async login(params: Record<string, unknown>) {
      try {
        const { state, token, message: msg } = await userApi.login(params)
        if (state === 200) {
          this.token = token
          localStorage.setItem('token', this.token)
        } else {
          message.error(msg)
        }
      } catch (error) {
        console.log(error)
        message.error('网络请求连接失败~')
      }
    },
    //存储当前的 机器人列表
    async SetProductRobotList(state: any) {
      this.ProductRobotList = state
    },

    async getUserInfo() {
      try {
        const { state, data, message: msg } = await userApi.getUserInfo()
        if (state === 200) {
          this.userInfo = { ...data }

          // let index = this.menuRole.indexOf("userPedigree");

          // if (data.role === 'ADMIN') {

          //   if (index === -1) {
          //     this.menuRole.push('userPedigree')
          //   }

          // } else {
          //   if (index !== -1) {
          //     this.menuRole.splice(index, 1);
          //   }
          // }

          // userApi.batchAddDefaultVerbalTrick()
        } else {
          message.error(msg)
        }
      } catch (error) {
        message.error('网络请求连接失败~')
      }
    },

    async logout() {
      this.token = ''
      this.userInfo = {}
      // this.menuRole = ['dashboard', 'account', 'goods', 'shop', 'order', 'afterSale', ]
      localStorage.clear()
    },
  },

  persist: {
    afterRestore: ctx => {
      // console.log('====', [...ctx.store.menuRole])

      const permissionStore = usePermissionStore()
      // permissionStore.initRoutes(['all'])
      permissionStore.initRoutes([...ctx.store.menuRole])
    },
    storage: sessionStorage,
  },
})
